package org.example.blog.service;

import org.example.blog.mapper.BlogInfoMapper;
import org.example.blog.mapper.UserInfoMapper;
import org.example.blog.model.BlogInfo;
import org.example.blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectUserInfoByName(String username) {
        return userInfoMapper.selectByName(username);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

//    public Integer selectAuthorIdByBlogId(Integer blogId) {
//        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
//        return blogInfo.getUserId();
//    }

    public UserInfo selectAuthorInfoByBlogId(Integer blogId) {
//        根据博客id，获取作者id
//        根据作者Id,获取作者信息
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        if (blogInfo == null || blogInfo.getUserId() == null) {
            return null;
        }
        UserInfo userInfo = userInfoMapper.selectById(blogInfo.getUserId());
        return userInfo;
    }
}
